package com.learning.sims.people;

import android.util.Log;

import com.learning.sims.World;
import com.learning.sims.status.EatStatus;
import com.learning.sims.status.GotoEatStatus;
import com.learning.sims.status.GotoMiningStatus;
import com.learning.sims.status.GotoSleepStatus;
import com.learning.sims.status.IStatus;
import com.learning.sims.status.MiningStatus;
import com.learning.sims.status.SleepStatus;
import com.learning.sims.utils.SimsUtil;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by xie on 2017/4/22.
 */

public class People {

    private Map<IStatus.Status,IStatus> mStatusMap;

    private IStatus mCurrentStatus = null;

    protected double energyMax = 100;
    protected double energy = 100;
    protected double money  = 0;
    protected double walkSpeed = 1;// m/s
    protected double walkSpendEnergyRate = 1;
    protected double sleepSpendEnergyRate = 1;
    protected double miningSpendEnergyRate = 1;
    protected double needAddEnergy = 10;


    public People() {
        initStatus();
        initPeopleParameter();
    }

    private void initStatus(){
        mStatusMap = new HashMap<IStatus.Status,IStatus>();
        mStatusMap.put(IStatus.Status.Sleep,new SleepStatus());
        mStatusMap.put(IStatus.Status.Mining,new MiningStatus());
        mStatusMap.put(IStatus.Status.Eat,new EatStatus());
        mStatusMap.put(IStatus.Status.GotoSleep,new GotoSleepStatus());
        mStatusMap.put(IStatus.Status.GotoEat,new GotoEatStatus());
        mStatusMap.put(IStatus.Status.GotoMining,new GotoMiningStatus());
    }

    //每个人物参数都有可能不同，所以参数可根据不同人物设定
    protected void initPeopleParameter(){

    }

    public void updateTime(long time, World world){
        if(mCurrentStatus == null){
            setStatus(SimsUtil.isSleepTime(time) ?
                        IStatus.Status.Sleep : isNeedAddEnergy() ?
                        IStatus.Status.GotoEat:IStatus.Status.GotoMining);
        }
        if(getEnergy() <= 0){
            world.removePeople(this);
            Log.d("Sims",getName()+" energy of 0");
        }else{
            mCurrentStatus.doSomething(time,this);
        }
    }

    public void setStatus(IStatus.Status status){
        mCurrentStatus = mStatusMap.get(status);
    }

    public double getEnergy() {
        return energy;
    }

    public double getMoney() {
        return money;
    }

    public double getWalkSpeed(){
        return walkSpeed;
    }

    public double getSleepSpendEnergyRate(){
        return sleepSpendEnergyRate;
    }

    public double getMiningSpendEnergyRate(){
        return miningSpendEnergyRate;
    }

    public double getWalkSpendEnergyRate(){
        return walkSpendEnergyRate;
    }

    public double getMaxEnergy(){
        return energyMax;
    }

    public boolean isNeedAddEnergy(){
        return getEnergy() <= needAddEnergy ? true:false ;
    }

    public void setEnergy(double energy) {
        this.energy = energy;
    }

    public void setMoney(double money) {
        this.money = money;
    }

    public String getName(){
        return "people";
    }

}
